package com.zretc.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.security.auth.message.callback.PrivateKeyCallback.Request;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zretc.entity.Area;
import com.zretc.entity.Singer;
import com.zretc.service.AreaService;
import com.zretc.service.SingerService;
import com.zretc.service.impl.AreaServiceImpl;
import com.zretc.service.impl.SingerServiceImpl;
import com.zretc.util.PageInfo;

/**
 * Servlet implementation class SingerServlet
 */
@WebServlet("/SingerServlet")
@MultipartConfig
public class SingerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private SingerService singerService = new SingerServiceImpl();
    private AreaService areaService = new AreaServiceImpl();
    private Gson gson = new Gson();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SingerServlet() {
        super();
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op = request.getParameter("op");
		if ("getSingerDetail".equals(op)) {
			doGetSingerDetail(request,response);
		}else if ("getSinger".equals(op)) {
			doGetSinger(request,response);
		}else if ("getArea".equals(op)) {
			doGetArea(request,response);
		}else if ("getGender".equals(op)) {
			doGetGender(request,response);
		}
	}
	
	//获取性别
	private void doGetGender(HttpServletRequest request, HttpServletResponse response) {
		
	}

	//获取地区
	private void doGetArea(HttpServletRequest request, HttpServletResponse response) throws IOException {
		List<Area> area = areaService.getArea();
		String json = gson.toJson(area);
		response.getWriter().write(json);
	}

	//获取歌手列表
	private void doGetSinger(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, String> map = new HashMap<>();
		String areaName = request.getParameter("areaName");
		String gender = request.getParameter("gender");
		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");
		map.put("areaName", areaName);
		map.put("gender", gender);
		map.put("pageNum", pageNum);
		map.put("pageSize", pageSize);
		PageInfo<Singer> singerByComdition = singerService.getSingerByComdition(map);
		String json = gson.toJson(singerByComdition);
		response.getWriter().write(json);
	}

	//获取歌手信息做前端渲染
	private void doGetSingerDetail(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, String> map = new HashMap<String, String>();
		String singerId = request.getParameter("singerId");
		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");
		map.put("singerId", singerId);
		map.put("pageNum", pageNum);
		map.put("pageSize", pageSize);
		PageInfo<Singer> singerBySingerId = singerService.getSingerBySingerId(map);
		String json = gson.toJson(singerBySingerId);
		response.getWriter().write(json);
	}

}
